<?php

namespace Admin\Controller;

class OtherController extends AdminController {

    public function gywm() {
        $id = I("id", 0, 'intval');
        if (IS_POST) {

            $data['update_time'] = date('Y-m-d H:i:s', time());
            $data['desc'] = I("desc");
            $data['key'] = 'guanyuwomen';

            if ($id == 0) {
                $res = M('Configure')->add($data);
            } else {
                $res = M('Configure')->where(array('id' => $id))->save($data);
            }

            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $data = M('Configure')->where(array("key" => "guanyuwomen"))->find();

            $this->assign('data', $data);
            $this->assign('meta_title', '关于我们');
            $this->display();
        }
    }

    public function kefurexian() {
        $id = I("id", 0, 'intval');
        if (IS_POST) {

            $data['update_time'] = date('Y-m-d H:i:s', time());
            $value['tel'] = I("tel");
            $value['qcode1'] = I("qcode1");
            $value['wx1'] = I("wx1");
            $value['qcode2'] = I("qcode2");
            $value['wx2'] = I("wx2");

            $data['key'] = 'kefurexian';

            $data['value'] = serialize($value);

            if ($id == 0) {
                $res = M('Configure')->add($data);
            } else {
                $res = M('Configure')->where(array('id' => $id))->save($data);
            }

            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $data = M('Configure')->where(array("key" => "kefurexian"))->find();

//            var_dump($data);die;
            $value = [];
            if (!empty($data)) {
                $value = unserialize($data['value']);
                $id = $data['id'];
            }

//            var_dump($value);die;

            $this->assign('id', $id);
            $this->assign('data', $value);
            $this->assign('meta_title', '平台客服热线');
            $this->display();
        }
    }

    public function lrgs() {
        $id = I("id", 0, 'intval');
        if (IS_POST) {

            $data['update_time'] = date('Y-m-d H:i:s', time());
            $value['icon'] = I("icon");

            $data['key'] = 'lrgs';

            $data['value'] = serialize($value);
            $data['desc'] = I('desc');

            if ($id == 0) {
                $res = M('Configure')->add($data);
            } else {
                $res = M('Configure')->where(array('id' => $id))->save($data);
            }

            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $data = M('Configure')->where(array("key" => "lrgs"))->find();

            $value = [];
            if (!empty($data)) {
                $value = unserialize($data['value']);
            }

            $value['desc'] = $data['desc'];
            $value['id'] = $data['id'];

            $this->assign('data', $value);
            $this->assign('meta_title', '懒人故事');
            $this->display();
        }
    }

    public function jifen() {
        if (IS_POST) {

            $jifendikou['update_time'] = date('Y-m-d H:i:s', time());
            $jifendikou['desc'] = I("jifendikou");
            $jifendikou['key'] = 'jifendikou';

            $res = M('Configure')->where(array("key" => "jifendikou"))->save($jifendikou);

            $fanjifen['update_time'] = date('Y-m-d H:i:s', time());
            $fanjifen['desc'] = I("fanjifen");
            $fanjifen['key'] = 'fanjifen';

            $res = M('Configure')->where(array("key" => "fanjifen"))->save($fanjifen);

            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $dikou = M('Configure')->where(array("key" => "jifendikou"))->getField('desc');
            $zengsong = M('Configure')->where(array("key" => "fanjifen"))->getField('desc');
            $this->assign('dikou', $dikou);
            $this->assign('zengsong', $zengsong);
            $this->assign('meta_title', '积分配置');
            $this->display();
        }
    }

    public function yhxy() {
        $id = I("id", 0, 'intval');
        if (IS_POST) {

            $data['update_time'] = date('Y-m-d H:i:s', time());
            $data['desc'] = I("desc");
            $data['key'] = 'yonghuxieyi';

            if ($id == 0) {
                $res = M('Configure')->add($data);
            } else {
                $res = M('Configure')->where(array('id' => $id))->save($data);
            }

            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $data = M('Configure')->where(array("key" => "yonghuxieyi"))->find();

            $this->assign('data', $data);
            $this->assign('meta_title', '用户协议');
            $this->display();
        }
    }

    public function kdsm() {
        $id = I("id", 0, 'intval');
        if (IS_POST) {

            $data['update_time'] = date('Y-m-d H:i:s', time());
            $data['desc'] = I("desc");
            $data['key'] = 'fenxiaoshuoming';

            if ($id == 0) {
                $res = M('Configure')->add($data);
            } else {
                $res = M('Configure')->where(array('id' => $id))->save($data);
            }

            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $data = M('Configure')->where(array("key" => "fenxiaoshuoming"))->find();

            $this->assign('data', $data);
            $this->assign('meta_title', '开店说明');
            $this->display();
        }
    }

    public function fenxiang() {
        $id = I("id", 0, 'intval');
        if (IS_POST) {
            $g['kaiguan'] = I("kaiguan");
            $g['img_url'] = I("img_url");
            $g['title'] = I("title");
            $g['subtitle'] = I("subtitle");
            $g['ios_download_url'] = I("ios_download_url");
            $g['android_download_url'] = I("android_download_url");

            $data['value'] = serialize($g);
            $data['update_time'] = date('Y-m-d H:i:s', time());
            $data['key'] = 'fenxiang';

            if ($id == 0) {
                $res = M('Configure')->add($data);
            } else {
                $res = M('Configure')->where(array('id' => $id))->save($data);
            }


            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $data = M('Configure')->where(array("key" => "fenxiang"))->find();

            $value = [];
            if (!empty($data)) {
                $value = unserialize($data['value']);
                $id = $data['id'];
            }

            $this->assign('id', $id);
            $this->assign('data', $value);
            $this->assign('meta_title', '分享设置');
            $this->display();
        }
    }

    public function opinion() {
        $where .= '1=1 ';

        $phone = I('phone');
        if ($phone) {
            $where .= " and u.phone = '" . $phone . "'";
            $this->assign('phone', $phone);
        }

        $content = I('content');
        if ($content) {
            $where .= " and o.content like '%" . $content . "%'";
            $this->assign('content', $content);
        }


        //日期选择
        $start_date = I("start_date");
        $end_date = I("end_date");


        if (!empty($end_date) && !empty($start_date)) {
            if ($end_date <= $start_date) {
                $this->error('开始时间不允许小于等于结束时间');
                exit;
            } else {
                $where .= " and o.create_time between '$start_date 00:00:00' AND '$end_date 23:59:59'";
//                $where['sp.create_time'] = array("between", array($start_date, $end_date . ' 23:59:59'));
            }
            $this->assign("start_date", $start_date);
            $this->assign("end_date", $end_date);
        } else {
            if (!empty($start_date)) {
//                $where['sp.create_time'] = array('EGT', $start_date);
                $where .= " and o.create_time >='$start_date 00:00:00'";
                $this->assign("start_date", $start_date);
            }

            if (!empty($end_date)) {
//                $where['sp.create_time'] = array('ELT', $end_date . ' 23:59:59');
                $where .= " and o.create_time <= '$end_date 23:59:59'";
                $this->assign("end_date", $end_date);
            }
        }
        $M = M()->table('ot_opinion as o ')
                ->join('ot_user as u on o.user_id = u.user_id');


        $field = 'o.*,u.nick_name,u.phone';

        $order = "o.create_time desc";
        $list = $this->lists($M, $where, $order, $field);

        $this->assign('_list', $list);
        $this->display();
    }

    public function check() {
        if (IS_POST) {
         
            $data['desc'] = I("kaiguan");

            $res = M('Configure')->where(array("key" => "check"))->save($data);

            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $data['check'] = M('Configure')->where(array("key" => "check"))->getField('desc');
            $this->assign('value',$data);
            $this->assign('meta_title', '应用审核开关');
            $this->display();
        }
    }

}
